//************************************************************
//
// REGSTEP 9.  REACTION FUNCTION WITH MEDIAN FIRM
//
// USING SIGMA LEVELS 2 DIGIT
//
// AUTHOR: LIONEL NESTA
//
// DATASOURCE: USING final_reaction_functions.dta FROM DATASTEP 5
//
//*************************************************************


// PREAMBLE
*set processors 24

clear
clear matrix 
clear mata
set more off
set mem 600m
set matsize 800
pwd
cd ..\RESULTS

// OPENING FILE

use ..\DATA\final_reaction_functions.dta, clear

* Choosing which case 
* case 1 : in growth rate for markets with high gamma 
* case 2 : in levels for markets with high gamma 
* case 3 : in growth rate for markets with low gamma 
* case 4 : in levels for markets with high gamma 

gen case = case2 

// DROPS ALL RF OUTSIDE CORNERS 1 TO 4 IN BETA-SIGMA SPACE 

collapse (p90) lnrd2_p90 = lnrd2 lnrdi2_p90 = lnrdi2 (mean) lnrd1 psm_lnrd1 psm_lnrdi1 lnrd2* mrd_sec1 mrd_sec2 mrd_sec1_fs mrd_sec2_fs c_techno_c c_market_c lnrdi1 lnk1 lny1 lnk12 LI1 LI12 lnliq_k1 lnliq_ratio1 msh1 msh12 LI2 LI22 lnrdpatsm1 patent1 lnrdi2 lnk2 lnk22 lnliq_ratio2 msh2 msh22 lnrdpatsm2 patent2 L1lnrd1 L1lnrd2 L1lnrdi1 L1lnrdi2 L1lnk1 lnliq_k2  L1lnliq_ratio1 L1msh1 L1lnrdpatsm1 L2lnrdpatsm1 L1lnrdpatsm2 L2lnrdpatsm2, by(gvkey1 year case)


drop if case == .

tabulate year, gen(year_)

egen fcid  = group(gvkey1 case)
tsset fcid year

drop if year < 1990

// IV EST

ivreg2 f.lnrd1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1 (lnrd2 = l.L1lnrd2 mrd_sec2_fs)  year_* if case == 1 , robust gmm2s ffirst
outreg2 lnrd2  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1  using RF_mean_ols_iv_psm_sigma_level, dec(3) bdec(3) adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) replace

ivreg2 lnrd1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1 (lnrd2 = l.L1lnrd2 mrd_sec2_fs)  year_* if case == 2 , robust gmm2s ffirst
outreg2 lnrd2  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1  using RF_mean_ols_iv_psm_sigma_level, dec(3) bdec(3)  adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

ivreg2 lnrd1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1 (lnrd2 = l.L1lnrd2 L1lnrdpatsm2  )  year_* if case == 3 , robust gmm2s ffirst
outreg2 lnrd2  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1  using RF_mean_ols_iv_psm_sigma_level, dec(3) bdec(3)  adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

ivreg2 lnrd1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1 (lnrd2 = L1lnrd2  L1lnrdpatsm2)  year_* if case == 4 , robust gmm2s ffirst
outreg2 lnrd2  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1  using RF_mean_ols_iv_psm_sigma_level, dec(3) bdec(3)  adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

// IV EST RDI

ivreg2 lnrdi1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrdi1 (lnrdi2 = L1lnrdi2 mrd_sec2)  year_* if case == 1 , robust gmm2s ffirst
outreg2 lnrdi2  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrdi1  using RF_mean_ols_iv_psm_sigma_level, dec(3) bdec(3) adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

ivreg2 lnrdi1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrdi1 (lnrdi2 = L1lnrdi2 L1lnrdpatsm2)  year_* if case == 2 , robust gmm2s ffirst
outreg2 lnrdi2  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrdi1  using RF_mean_ols_iv_psm_sigma_level, dec(3) bdec(3)  adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

ivreg2 lnrdi1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrdi1 (lnrdi2 = L1lnrdi2 mrd_sec2)  year_* if case == 3 , robust gmm2s ffirst
outreg2 lnrdi2  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrdi1  using RF_mean_ols_iv_psm_sigma_level, dec(3) bdec(3)  adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

ivreg2 lnrdi1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrdi1 (lnrdi2 = L1lnrd2 L1lnrdpatsm2)  year_* if case == 4 , robust gmm2s ffirst
outreg2 lnrdi2  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrdi1  using RF_mean_ols_iv_psm_sigma_level, dec(3) bdec(3)  adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

// IV EST P90

ivreg2 lnrd1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1 (lnrd2_p90 = L1lnrd2 L1lnrdpatsm2)  year_* if case == 1 , robust gmm2s ffirst
outreg2 lnrd2_p90  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1  using RF_mean_ols_iv_psm_sigma_level_p90, dec(3) bdec(3) adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) replace

ivreg2 lnrd1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1 (lnrd2_p90 = L1lnrd2 L1lnrdpatsm2)  year_* if case == 2 , robust gmm2s ffirst
outreg2 lnrd2_p90  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1  using RF_mean_ols_iv_psm_sigma_level_p90, dec(3) bdec(3)  adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

ivreg2 lnrd1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1 (lnrd2_p90 = l.L1lnrd2 L1lnrdpatsm2)  year_* if case == 3 , robust gmm2s ffirst
outreg2 lnrd2_p90  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1  using RF_mean_ols_iv_psm_sigma_level_p90, dec(3) bdec(3)  adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

ivreg2 lnrd1 lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1 (lnrd2_p90 = L1lnrd2 L1lnrdpatsm2)  year_* if case == 4 , robust gmm2s ffirst
outreg2 lnrd2_p90  lnk1 lnliq_ratio1 L1lnrdpatsm1 psm_lnrd1  using RF_mean_ols_iv_psm_sigma_level_p90, dec(3) bdec(3)  adds(Hansen J, e(j), Hansen cp, e(jp), Kleibergen-Paap Wald F statistic, e(widstat)) tex excel word aster(se) ctitle(PSM_IV) append

